<?php

class Soul_Request_Application_Command extends Soul_Request_Application_Base {
	
	public function __construct(){
		parent::__construct();
	}

	protected function beforeInitialize(){
		$this->dispatchEvent('request.beforeInitialize'); 
	}
	
    protected function initialize() {    	
        $this->beforeInitialize();
		$GLOBALS[RC] = Soul_Request_Context::getInstance();
        $this->afterInitialize();
    }
    
	protected function afterInitialize(){
		$this->dispatchEvent('request.afterInitialize');
	}    

	protected function beforeAuthentication(){
		$this->dispatchEvent('request.beforeAuthentication');
	}	
	
	protected function authentication(){
		$this->beforeAuthentication();
		$this->afterAuthentication();
	}
	
	protected function afterAuthentication(){
		$this->dispatchEvent('request.afterAuthentication');
	}	
	
	protected function beforeHandle(){
		$this->dispatchEvent('request.beforeHandle');
	}
    
    protected function handle(){
    	$this->beforeHandle();
		Soul_Request_Factory::getHandler()->handle();
		$this->afterHandle();
    }
    
	protected function afterHandle(){
		$this->dispatchEvent('request.afterHandle');
	}   
	
	protected function beforeOutput(){
		$this->dispatchEvent('request.beforeOutput');
	}
    
    protected function output(){
    	$this->beforeOutput();
    	$this->getContext()->getResponse()->flush();
    	$this->afterOutput();
    }
    
	protected function afterOutput(){
    	$this->dispatchEvent('request.afterOutput');
	}    
    
    protected function finish() {
		
    }    

    public function run() {
		$this->initialize();
		$this->sessionStart();
		$this->authentication();
		$this->handle();
		$this->output();
		$this->finish();
    }
}